<template>
  <!-- 登录页 -->
  <div id="app">
    <div class="container">
      <div class="close">
        <span class="iconfont iconicon-test"> </span>
      </div>
      <div class="logo">
        <span class="iconfont iconnew"> </span>
      </div>
      <div class="inputs">
        <div class="input">
          <hminput
            placeholder="用户名/手机号"
            msg="用户名/手机号错误"
            v-model="user.username"
            :rules="
              /^([\u4E00-\u9FA5A-Za-z]+$)|(^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$)/
            "
            type="text"
          ></hminput>
          <hminput
            placeholder="密码"
            msg="密码错误"
            v-model="user.password"
            type="password"
          ></hminput>
        </div>
      </div>
      <p class="tips">
        没有账号？
        <a href="/register" class="">去注册</a>
      </p>
      <hmbutton type="green" @click="login">
        <span>登录</span>
      </hmbutton>
    </div>
  </div>
</template>

<script>
import hmbutton from "@/components/hmbutton.vue";
import hminput from "@/components/hminput.vue";

import { mylogin } from "@/apis/user.js";
export default {
  data() {
    return {
      user: {
        username: "13899999999",
        password: "123456789",
      },
    };
  },
  methods: {
    async login(e) {
      // mylogin(this.user)
      //   .then((data) => {
      //     console.log(data);
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });
      if (
        /^(\d{5,6})$|^(1\d{10})$/.test(this.user.username) &&
        /^\S{3,16}$/.test(this.user.password)
      ) {
        try {
          let res = await mylogin(this.user);
          // console.log(res);
          if (res.data.statusCode == 200) {
            this.$toast(res.data.message);
            localStorage.setItem("mytoken", res.data.data.token);
            localStorage.setItem("mimas", JSON.stringify(res.config.data));
            // this.$router.push({ name: "personal" });
            localStorage.setItem("id", JSON.stringify(res.data.data.user.id));
            // this.$router.push({ path: "/personal/" + res.data.data.user.id });
            let redirecturl = location.href.split("=")[1];
            redirecturl = decodeURIComponent(redirecturl);
            console.log(redirecturl);
            if (redirecturl && redirecturl !== "undefined") {
              this.$router.back();
            } else {
              this.$router.push({ path: "/personal/" + res.data.data.user.id });
            }
          }
        } catch (err) {
          this.$toast("用户名或密码输入不正确");
        }
      } else {
        this.$toast("用户名或密码输入不正确");
      }
    },
  },
  components: {
    hmbutton,
    hminput,
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}

.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}

.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}

.inputs {
  input {
    margin-bottom: 20px;
    padding: 0;
  }
}

.tips {
  text-align: right;
  margin-bottom: 20px;

  a {
    color: #3385ff;
  }
}
.container {
  background-color: #eaeaea;
  height: 100vh;
}
</style>